package net.cherrylab.war.view
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	
	import mx.core.UIComponent;
	
	import net.cherrylab.war.model.WarModelLocater;
	
	import spark.components.Group;
	
	public class Checkerboard extends Group
	{
		
		[Bindable]
		private var model:WarModelLocater=WarModelLocater.getInstance()
		private var _radius:Number=50;    //半径
		private var _horizontal:int=10;     
		private var _vertical:int=10;
		private var _items:Array=model.gridArray;
		private var hexagon:Hexagon;
		private var bmphex:BitmapData;
		
		public function Checkerboard(v:int,h:int)
		{
			this.vertical=v;
			this.horizontal=h;
			hexagon=new Hexagon(_radius);
			bmphex=hexagon.bmp;
			
			init();
		}

		private function init():void
		{
			_items=new Array();
			
			for(var v:int=0;v<_vertical;v++)
			{
				_items[v]=new Array();
				for(var h:int=0;h<_horizontal;h++)
				{
					var grid:HexagonGrid=new HexagonGrid(_radius);
					var bmp:Bitmap=new Bitmap(bmphex);
					grid.ui.addChild(bmp);
					
					grid.code=v.toString()+"-"+h.toString();
					//x轴坐标
					grid.x=grid.radiusH*(v+1)*3/2-grid.radiusH/2;
					//y轴坐标
					grid.y=grid.radiusV*((h+1)*2-1)+(v%2)*grid.radiusV;
					grid.signX=v;
					grid.signY=h;
					this.addElement(grid);
					grid.addEventListener(MouseEvent.CLICK,onClick);
					_items[v][h]=grid;
				}
			}
		}
		
		private function onClick(event:MouseEvent):void
		{
			model.selectedGrid=event.currentTarget as HexagonGrid;
		}
		
		public function set horizontal(v:int):void
		{
			_horizontal=v;
		}
		public function get horizontal():int
		{
			return _horizontal;
		}
		public function set vertical(v:int):void
		{
			_vertical=v;
		}
		public function get vertical():int
		{
			return _vertical;
		}
		/**
		 * 获取格子数组
		 * */
		public function get items():Array
		{
			return _items;
		}
		/**
		 * 获取半径
		 * */
		public function get radius():Number
		{
			return _radius;
		}
		public function set radius(value:Number):void
		{
			_radius = value;
		}
	}
}